package com.web.dao;

import com.github.pagehelper.Page;
import com.web.controller.web.WebMemberController;
import com.web.entity.Member;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author FDjavaone
 */
public interface MemberMapper {


    @Delete({
            "delete from t_member ",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
            "insert into t_member (phone, name, ",
            "password, member_type, ",
            "identity_no, auth_status, ",
            "header_img, invite_code, ",
            "invited_code, nick_name, ",
            "gender, pay_password,consume_amt,bonus_amt,level,enabled, ",
            "create_time, modify_time)",
            "values (#{phone,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, ",
            "#{password,jdbcType=VARCHAR}, #{memberType,jdbcType=VARCHAR}, ",
            "#{identityNo,jdbcType=VARCHAR}, #{authStatus,jdbcType=INTEGER}, ",
            "#{headerImg,jdbcType=VARCHAR}, #{inviteCode,jdbcType=VARCHAR}, ",
            "#{invitedCode,jdbcType=VARCHAR}, #{nickName,jdbcType=VARCHAR}, ",
            "#{gender,jdbcType=VARCHAR}, #{payPassword,jdbcType=VARCHAR}, #{consumeAmt,jdbcType=DECIMAL}, #{bonusAmt,jdbcType=DECIMAL}, #{level,jdbcType=INTEGER},#{enabled,jdbcType=INTEGER},",
            "#{createTime,jdbcType=TIMESTAMP}, #{modifyTime,jdbcType=TIMESTAMP})"
    })
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = Long.class)
    int insert(Member record);

    @InsertProvider(type = MemberSqlProvider.class, method = "insertSelective")
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = Long.class)
    int insertSelective(Member record);


    @Select({
            "select ",
            "id, phone, name, password, member_type, identity_no, auth_status, header_img, ",
            "invite_code, invited_code, nick_name, gender, pay_password,consume_amt,bonus_amt,level,enabled, create_time, modify_time ",
            "from t_member",
            "where id = #{id,jdbcType=BIGINT}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "phone", property = "phone", jdbcType = JdbcType.VARCHAR),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "password", property = "password", jdbcType = JdbcType.VARCHAR),
            @Result(column = "member_type", property = "memberType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "identity_no", property = "identityNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "auth_status", property = "authStatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "header_img", property = "headerImg", jdbcType = JdbcType.VARCHAR),
            @Result(column = "invite_code", property = "inviteCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "invited_code", property = "invitedCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "nick_name", property = "nickName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "gender", property = "gender", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pay_password", property = "payPassword", jdbcType = JdbcType.VARCHAR),
            @Result(column = "consume_amt", property = "consumeAmt", jdbcType = JdbcType.DECIMAL),
            @Result(column = "bonus_amt", property = "bonusAmt", jdbcType = JdbcType.DECIMAL),
            @Result(column = "level", property = "level", jdbcType = JdbcType.INTEGER),
            @Result(column = "enabled", property = "enabled", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "modify_time", property = "modifyTime", jdbcType = JdbcType.TIMESTAMP)
    })
    Member selectByPrimaryKey(Long id);


    @UpdateProvider(type = MemberSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Member record);

    @Update({
            "update t_member ",
            " set phone = #{phone,jdbcType=VARCHAR},",
            " name = #{name,jdbcType=VARCHAR},",
            " password = #{password,jdbcType=VARCHAR},",
            " member_type = #{memberType,jdbcType=VARCHAR},",
            " identity_no = #{identityNo,jdbcType=VARCHAR},",
            " auth_status = #{authStatus,jdbcType=INTEGER},",
            " header_img = #{headerImg,jdbcType=VARCHAR},",
            " invite_code = #{inviteCode,jdbcType=VARCHAR},",
            " invited_code = #{invitedCode,jdbcType=VARCHAR},",
            " nick_name = #{nickName,jdbcType=VARCHAR},",
            " gender = #{gender,jdbcType=VARCHAR},",
            " pay_password = #{payPassword,jdbcType=VARCHAR},",
            " consume_amt = #{consumeAmt,jdbcType=DECIMAL},",
            " bonus_amt = #{bonusAmt,jdbcType=DECIMAL},",
            " level = #{level,jdbcType=INTEGER},",
            " enabled = #{enabled,jdbcType=INTEGER},",
            " create_time = #{createTime,jdbcType=TIMESTAMP},",
            " modify_time = #{modifyTime,jdbcType=TIMESTAMP}",
            " where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(Member record);

    @Select({
            "  select",
            " id, phone, name, password, member_type, identity_no, auth_status, header_img, ",
            " invite_code, invited_code, nick_name, gender, pay_password,consume_amt,bonus_amt,level,enabled, create_time, modify_time",
            " from t_member",
            " where phone = #{phone,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "phone", property = "phone", jdbcType = JdbcType.VARCHAR),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "password", property = "password", jdbcType = JdbcType.VARCHAR),
            @Result(column = "member_type", property = "memberType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "identity_no", property = "identityNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "auth_status", property = "authStatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "header_img", property = "headerImg", jdbcType = JdbcType.VARCHAR),
            @Result(column = "invite_code", property = "inviteCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "invited_code", property = "invitedCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "nick_name", property = "nickName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "gender", property = "gender", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pay_password", property = "payPassword", jdbcType = JdbcType.VARCHAR),
            @Result(column = "consume_amt", property = "consumeAmt", jdbcType = JdbcType.DECIMAL),
            @Result(column = "bonus_amt", property = "bonusAmt", jdbcType = JdbcType.DECIMAL),
            @Result(column = "level", property = "level", jdbcType = JdbcType.INTEGER),
            @Result(column = "enabled", property = "enabled", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "modify_time", property = "modifyTime", jdbcType = JdbcType.TIMESTAMP)
    })
    Member selectByMobile(String phone);

    @SelectProvider(type = MemberSqlProvider.class, method = "selectByMember")
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "phone", property = "phone", jdbcType = JdbcType.VARCHAR),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "password", property = "password", jdbcType = JdbcType.VARCHAR),
            @Result(column = "member_type", property = "memberType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "identity_no", property = "identityNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "auth_status", property = "authStatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "header_img", property = "headerImg", jdbcType = JdbcType.VARCHAR),
            @Result(column = "invite_code", property = "inviteCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "invited_code", property = "invitedCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "nick_name", property = "nickName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "gender", property = "gender", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pay_password", property = "payPassword", jdbcType = JdbcType.VARCHAR),
            @Result(column = "consume_amt", property = "consumeAmt", jdbcType = JdbcType.DECIMAL),
            @Result(column = "bonus_amt", property = "bonusAmt", jdbcType = JdbcType.DECIMAL),
            @Result(column = "level", property = "level", jdbcType = JdbcType.INTEGER),
            @Result(column = "enabled", property = "enabled", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "modify_time", property = "modifyTime", jdbcType = JdbcType.TIMESTAMP)
    })
    Member selectByMember(Member m);

    @Select({
            "select",
            " id, phone, name, password, member_type, identity_no, auth_status, header_img, ",
            " invite_code, invited_code, nick_name, gender, pay_password,consume_amt,bonus_amt,level,enabled, create_time, modify_time",
            " from t_member",
            " where invited_code = #{inviteCode,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "phone", property = "phone", jdbcType = JdbcType.VARCHAR),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "password", property = "password", jdbcType = JdbcType.VARCHAR),
            @Result(column = "member_type", property = "memberType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "identity_no", property = "identityNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "auth_status", property = "authStatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "header_img", property = "headerImg", jdbcType = JdbcType.VARCHAR),
            @Result(column = "invite_code", property = "inviteCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "invited_code", property = "invitedCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "nick_name", property = "nickName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "gender", property = "gender", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pay_password", property = "payPassword", jdbcType = JdbcType.VARCHAR),
            @Result(column = "consume_amt", property = "consumeAmt", jdbcType = JdbcType.DECIMAL),
            @Result(column = "bonus_amt", property = "bonusAmt", jdbcType = JdbcType.DECIMAL),
            @Result(column = "level", property = "level", jdbcType = JdbcType.INTEGER),
            @Result(column = "enabled", property = "enabled", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "modify_time", property = "modifyTime", jdbcType = JdbcType.TIMESTAMP)
    })
    List<Member> selectByInvitedCode(String inviteCode);

    @Select({
            "select",
            " id, phone, name, password, member_type, identity_no, auth_status, header_img, ",
            " invite_code, invited_code, nick_name, gender, pay_password,consume_amt,bonus_amt,level,enabled, create_time, modify_time",
            " from t_member",
            " where invite_code = #{inviteCode,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "phone", property = "phone", jdbcType = JdbcType.VARCHAR),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "password", property = "password", jdbcType = JdbcType.VARCHAR),
            @Result(column = "member_type", property = "memberType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "identity_no", property = "identityNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "auth_status", property = "authStatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "header_img", property = "headerImg", jdbcType = JdbcType.VARCHAR),
            @Result(column = "invite_code", property = "inviteCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "invited_code", property = "invitedCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "nick_name", property = "nickName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "gender", property = "gender", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pay_password", property = "payPassword", jdbcType = JdbcType.VARCHAR),
            @Result(column = "consume_amt", property = "consumeAmt", jdbcType = JdbcType.DECIMAL),
            @Result(column = "bonus_amt", property = "bonusAmt", jdbcType = JdbcType.DECIMAL),
            @Result(column = "level", property = "level", jdbcType = JdbcType.INTEGER),
            @Result(column = "enabled", property = "enabled", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "modify_time", property = "modifyTime", jdbcType = JdbcType.TIMESTAMP)
    })
    Member selectMyUp(String inviteCode);

    @Select({
            "select count(1)",
            " from t_member",
            " where invited_code = #{inviteCode,jdbcType=VARCHAR} and level=#{i,jdbcType=INTEGER}"
    })
    Integer countMySub(@Param("invite_code")String invite_code, @Param("i")int i);

    @Update({
            "update t_member",
            " set ",
            " consume_amt =consume_amt+#{totalPrice,jdbcType=DECIMAL}",
            " where id = #{id,jdbcType=BIGINT}"
    })
    void consumeAmtAdd(@Param("totalPrice") BigDecimal totalPrice, @Param("id") Long id);

    @Update({
            "update t_member",
            " set ",
            " level =#{memberLevel,jdbcType=INTEGER}",
            " where id = #{id,jdbcType=BIGINT}"
    })
    void levelUp(@Param("memberLevel")int memberLevel,@Param("id") Long id);

    @Update({
            "update t_member",
            " set ",
            " bonus_amt =bonus_amt+#{multiply,jdbcType=DECIMAL}",
            " where id = #{id,jdbcType=BIGINT}"
    })
    void bounsAmtAdd(@Param("multiply")BigDecimal multiply,@Param("id") Long id);

    @Select({
            "select count(0) from t_member where member_type='common'"
    })
    Integer countCommon();

    @Select({
            "select count(0) from t_member where member_type='original'"
    })
    Integer countOri();



    @SelectProvider(type = MemberSqlProvider.class, method = "selectAllMembers")
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "phone", property = "phone", jdbcType = JdbcType.VARCHAR),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "password", property = "password", jdbcType = JdbcType.VARCHAR),
            @Result(column = "member_type", property = "memberType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "identity_no", property = "identityNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "auth_status", property = "authStatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "header_img", property = "headerImg", jdbcType = JdbcType.VARCHAR),
            @Result(column = "invite_code", property = "inviteCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "invited_code", property = "invitedCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "nick_name", property = "nickName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "gender", property = "gender", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pay_password", property = "payPassword", jdbcType = JdbcType.VARCHAR),
            @Result(column = "consume_amt", property = "consumeAmt", jdbcType = JdbcType.DECIMAL),
            @Result(column = "bonus_amt", property = "bonusAmt", jdbcType = JdbcType.DECIMAL),
            @Result(column = "level", property = "level", jdbcType = JdbcType.INTEGER),
            @Result(column = "enabled", property = "enabled", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "modify_time", property = "modifyTime", jdbcType = JdbcType.TIMESTAMP)
    })
    Page<Member> selectAllMembers(WebMemberController.MemberColumns memberColumns);


    @Select({
            "select",
            " id",
            " from t_member",
            ""
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
    })
    List<Member> selectAllMemeberId();

    @Select({
            "select",
            " id, phone, name, password, member_type, identity_no, auth_status, header_img,",
            " invite_code, invited_code, nick_name, gender, pay_password,consume_amt,bonus_amt,level,enabled, create_time, modify_time",
            " from t_member",
            " where identity_no = #{identityNo,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "phone", property = "phone", jdbcType = JdbcType.VARCHAR),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "password", property = "password", jdbcType = JdbcType.VARCHAR),
            @Result(column = "member_type", property = "memberType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "identity_no", property = "identityNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "auth_status", property = "authStatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "header_img", property = "headerImg", jdbcType = JdbcType.VARCHAR),
            @Result(column = "invite_code", property = "inviteCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "invited_code", property = "invitedCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "nick_name", property = "nickName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "gender", property = "gender", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pay_password", property = "payPassword", jdbcType = JdbcType.VARCHAR),
            @Result(column = "consume_amt", property = "consumeAmt", jdbcType = JdbcType.DECIMAL),
            @Result(column = "bonus_amt", property = "bonusAmt", jdbcType = JdbcType.DECIMAL),
            @Result(column = "level", property = "level", jdbcType = JdbcType.INTEGER),
            @Result(column = "enabled", property = "enabled", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "modify_time", property = "modifyTime", jdbcType = JdbcType.TIMESTAMP)
    })
    Member selectByIdentity(@Param("identityNo")String identityNo);


}